package cn.cai.pethome.base.util;


import cn.cai.pethome.base.dto.LogininfoDto;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.security.PublicKey;

@Component
public class UserContext {

    @Value("${JWT.publicKey}")
    private String publicKey;


    public LogininfoDto getUser(String JWTToken){
        try {
            //2.JWT工具类解析成对象
            //2.1.利用公钥解密
            PublicKey publicKey = RsaUtils.getPublicKey(JwtUtils.class.getClassLoader().getResource(this.publicKey).getFile());
            Payload<LogininfoDto> payload = JwtUtils.getInfoFromToken(JWTToken, publicKey, LogininfoDto.class);

            //2.2.从载荷身上获取数据
            LogininfoDto logininfoDto = payload.getUserInfo();
            return logininfoDto;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
